package com.linchong.concurrency.chapter7;

/**
 * @Author:linchong
 * @CreateTime:2020-01-07 09:03
 * @Description:class锁
 */
public class SynchronizedStatic {

	/**
	 *
	 * static 代码块只会被初始化一次
	 * Monitor锁住的是当前实例
	 * 静态代码块锁住的是class锁
	 * */
	static {
		synchronized (SynchronizedStatic.class) {
			try {
				Thread.sleep(10_000);
				System.out.println("static:" + Thread.currentThread().getName());
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public synchronized static void m1() {
		System.out.println("m1:" + Thread.currentThread().getName());
		try {
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public synchronized static void m2() {
		System.out.println("m2:" + Thread.currentThread().getName());
		try {
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	public static void m3() {
		System.out.println("m3:" + Thread.currentThread().getName());
		try {
			Thread.sleep(10_000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
